package com.mdtx.business.controller;

import com.mdtx.base.SystemErrorCode;
import com.mdtx.base.security.cache.LocalTokenCache;
import com.mdtx.base.security.context.AccessTokenContext;
import com.mdtx.base.security.type.AudienceType;
import com.mdtx.business.BusinessErrorCode;
import com.mdtx.business.entity.query.SignInQuery;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.mdtx.base.ResultData;
import com.mdtx.base.security.AccessToken;

import java.util.Date;

@Api(value = "sys",description = "登录，注册，退出")
@RestController
@RequestMapping("/sys")
public class SignController {
    private Logger log = LoggerFactory.getLogger(SignController.class);


    @ApiOperation(value = "手机端登录",notes = "手机端登录")
    @ApiResponses(value = {
            @ApiResponse(code= 200,message = "测试")
    })
    @RequestMapping(value  = "/signIn", method = RequestMethod.POST)
    public ResultData<String> signIn(@ApiParam(name="siq",value = "登录对象",required = true) @RequestBody SignInQuery siq) {


        String token  = AccessToken.generateToke();
        String accessToken = AccessToken.createAccessTokenPhone(token);

        AccessTokenContext context = new AccessTokenContext();
        context.setToken(token);
        context.setTokenType(AudienceType.PHONE_USER);
        context.setUpdateTime(new Date());
        LocalTokenCache.setAccessTokenContext(context);

        ResultData rd = new ResultData();
        rd.setData(accessToken);

        return rd;
    }

    @ApiOperation(value = "后台登录",notes = "后台登录")
  //  @ApiImplicitParam(name="SignInQuery",value = "登录对象",required = true,dataType = "SignInQuery",paramType = "body")
    @RequestMapping(value  = "/signInPc", method = RequestMethod.POST)
    public ResultData<String> signInPc( @RequestBody SignInQuery siq) {


        String token  = AccessToken.generateToke();
        String accessToken = AccessToken.createAccessTokenPhone(token);

        AccessTokenContext context = new AccessTokenContext();
        context.setToken(token);
        context.setTokenType(AudienceType.PHONE_USER);
        context.setUpdateTime(new Date());
        LocalTokenCache.setAccessTokenContext(context);

        ResultData rd = new ResultData();
        rd.setData(accessToken);

        return rd;
    }


}



